package cn.changeforyou.base.model;

import cn.changeforyou.base.exception.ExceptionFactory;
import cn.changeforyou.base.exception.SystemExceptionEnum;

/**
 * 所有模块的异常码都分配1000个值
 */
public enum Module {

    SYSTEM_MODULE(0),
    DATA_MODULE(1),
    AUTH_SERVICE(2),
    BLOG_SERVICE(3),
    DEV_UTIL_API(4),
    DB_MANAGER(5),
    DAO_FRAMEWORK(6),
    ;
    private int num;
    private int code;
    private int part = 1000;

    Module(int num) {
        this.code = num * part;
        this.num = num;
    }

    public int getCode() {
        return code;
    }

    public static Module getModuleByErrorCode(int code){
        Module[] values = Module.values();
        for (Module value : values) {
            int i = code / value.part;
            if(i == value.num) {
                return value;
            }
        }
        throw ExceptionFactory.jsonException(SystemExceptionEnum.NO_MODULE_INFO);
    }
}
